/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores
 * CA 94065 USA or visit www.oracle.com if you need additional information or
 * have any questions.
 */


package com.codename1.designer;

import java.util.prefs.Preferences;

/**
 * Allows changing the background checkerboard of the images wshown in the preview
 * UI's through ImagePanel and CodenameOneImageRenderer
 *
 * @author Shai Almog
 */
public class CheckerBoardColorCalibration extends javax.swing.JDialog {

    /** Creates new form CheckerBoardColorCalibration */
    public CheckerBoardColorCalibration(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        ModifiableJOptionPane.reverseOKCancel(ok, cancel);
        setLocationByPlatform(true);
        colorA.setText(Integer.toHexString(getColorA()));
        colorB.setText(Integer.toHexString(getColorB()));
        ColorIcon.installWithColorPicker(colorAButton, colorA);
        ColorIcon.installWithColorPicker(colorBButton, colorB);
    }

    public static int getColorA() {
        return Preferences.userNodeForPackage(CheckerBoardColorCalibration.class).getInt("checkerboardA", 15724527);
    }

    public static int getColorB() {
        return Preferences.userNodeForPackage(CheckerBoardColorCalibration.class).getInt("checkerboardB", 10066329);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        colorA = new javax.swing.JTextField();
        colorAButton = new javax.swing.JButton();
        lbl = new javax.swing.JLabel();
        colorB = new javax.swing.JTextField();
        colorBButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();

        FormListener formListener = new FormListener();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Color A");
        jLabel1.setName("jLabel1"); // NOI18N

        colorA.setColumns(8);
        colorA.setText("EFEFEF");
        colorA.setName("colorA"); // NOI18N

        colorAButton.setText("...");
        colorAButton.setName("colorAButton"); // NOI18N

        lbl.setText("Color B");
        lbl.setName("lbl"); // NOI18N

        colorB.setColumns(8);
        colorB.setText("999999");
        colorB.setName("colorB"); // NOI18N

        colorBButton.setText("...");
        colorBButton.setName("colorBButton"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridLayout());

        ok.setText("OK");
        ok.setName("ok"); // NOI18N
        ok.addActionListener(formListener);
        jPanel2.add(ok);

        cancel.setText("Cancel");
        cancel.setName("cancel"); // NOI18N
        cancel.addActionListener(formListener);
        jPanel2.add(cancel);

        jPanel1.add(jPanel2);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(lbl))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(colorA, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(colorAButton))
                            .add(layout.createSequentialGroup()
                                .add(colorB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(colorBButton)))))
                .addContainerGap())
        );

        layout.linkSize(new java.awt.Component[] {colorA, colorB}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(colorA, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(colorAButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(lbl)
                    .add(colorB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(colorBButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        FormListener() {}
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == ok) {
                CheckerBoardColorCalibration.this.okActionPerformed(evt);
            }
            else if (evt.getSource() == cancel) {
                CheckerBoardColorCalibration.this.cancelActionPerformed(evt);
            }
        }
    }// </editor-fold>//GEN-END:initComponents

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        dispose();
        Preferences.userNodeForPackage(getClass()).putInt("checkerboardA", Integer.parseInt(colorA.getText(), 16));
        Preferences.userNodeForPackage(getClass()).putInt("checkerboardB", Integer.parseInt(colorB.getText(), 16));
}//GEN-LAST:event_okActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        dispose();
    }//GEN-LAST:event_cancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel;
    private javax.swing.JTextField colorA;
    private javax.swing.JButton colorAButton;
    private javax.swing.JTextField colorB;
    private javax.swing.JButton colorBButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lbl;
    private javax.swing.JButton ok;
    // End of variables declaration//GEN-END:variables

}
